/*
 * Copyright (c) 2008-2009, Computational Crawling LP
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
 *
 *    * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
 *    * Neither the name of Computational Crawling LP, 80legs, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.eightylegs.customer.sample;

import java.util.Collection;
import java.util.Map;
import java.util.Properties;

import com.eightylegs.customer.Default80AppPropertyKeys;
import com.eightylegs.customer.I80App;

/**
 * Class Sample80App.
 * 
 * In the example code below, we are finding all .html 
 * pages on www.techcrunch.com that mention "80legs" or "80 legs". 
 */
public class Sample80App implements I80App {
	
/*
	 // EXAMPLE CODE
	 private String[] dataStrings;
 */		 
	
	/**
  	 * This should be set manually to the version name of the interface you have implemented.
  	 * 
  	 * @return the version name of the interface you have implemented
	 */
	public String getVersion() {
		return "80App_1.2";  // this must be set correctly or 80legs will not use your jar.
	}
	
	/**
	 * This initializes the Sample80App object. You should
	 * deserialize your data here if necessary and also store the
	 * Properties object if you are passing user parameter this way.
	 * 
	 * The properties file is not used in release 0.8, but it will be used
	 * in a future version and the customer will be allowed to pass user 
	 * settings to their code this way.
	 * 
	 * Your data sets will be uploaded to 80legs servers either 
	 * through the 80legs Portal or by using the 80legs Crawl API.
	 * 
	 * @param properties   This contains the user selected
	 *                     parameters from the web 
	 *                     portal or interface for this job
	 * @param data   The data that you passed into the 80legs web
	 *               portal.  You should deserialize that data here
	 *               and store anything necessary as a private object
	 *               in your I80App class.
	 */
	public void initialize( Properties properties, byte[] data ) {
		//TODO: Fill in your constructor code, if you wish.
		
/*
		 // EXAMPLE CODE
		
 		 // Save the data strings 
		 try {
			 String inputDataString = byteArrayToString(data, 0, data.length);
			 dataStrings = inputDataString.split(",");
		 }
		 catch ( Exception e ) {
		 }
 */		 
		 
	}
	
	/**
	 * The purpose of this method is to parse links from the 
	 * content of a single document from the web.  This could be 
	 * any sort of document, including HTML, JPG, SWF, etc.
	 * Each document matching your specified 80legs crawl 
	 * parameters will be passed into this method.
	 * The byte content (pageContent parameter) passed into 
	 * this  method is ALWAYS DECOMPRESSED and DECHUNKED.
	 * 
	 * @param documentContent   The byte content of the 
	 *                          document to be processed.  
	 *                          This could be any sort 
	 *                          of document content, 
	 *                          including HTML, JPG, SWF, 
	 *                          etc.
	 * @param url   The URL of the document to be processed, 
	 *              in UTF-8 String representation.
	 * @param headers   A HashMap of the HTTP headers 
	 *                  retrieved with this document. 
	 *                  The HashMap key is the name of the 
	 *                  header and the HashMap value is 
	 *                  the header value.  All keys and 
	 *                  values are UTF-8 String 
	 *                  representations.
	 * @param statusCodeLine  The first line from the HTTP
	 *                        response for this document, 
	 *                        including the protocol, status
	 *                        code, and status description
	 *                        (i.e., "HTTP/1.1 200 OK"), as
	 *                        a UTF-8 String.
	 * 
	 * @return   The links to be processed.  Return an empty collection
	 *           (e.g. return new ArrayList<String>();) if you do not want
	 *           to process any links from the document.  Return null if
	 *           you want to use the default 80legs link processing
	 */
	public Collection<String> parseLinks(byte[] documentContent, String url, Map<String, String> headers, Map<Default80AppPropertyKeys, Object> default80AppProperties, String statusCodeLine) {
		// TODO: Fill in your code.
		
		// TODO: Return your links to be processed
		return null;  // returning null means that 80legs will use its own default parseLinks()
		// return new ArrayList<String>();  // returning an empty string means that no links will be processed from this document
	}
	
	
	/**
	 * The purpose of this method is to analyze the content 
	 * of a single document from the web.  This could be 
	 * any sort of document, including HTML, JPG, SWF, etc.
	 * Each document matching your specified 80legs analysis 
	 * parameters will be passed into this method.
	 * The byte content (pageContent parameter) passed into 
	 * this  method is ALWAYS DECOMPRESSED and DECHUNKED.
	 * 
	 * @param documentContent   The byte content of the 
	 *                          document to be processed.  
	 *                          This could be any sort 
	 *                          of document content, 
	 *                          including HTML, JPG, SWF, 
	 *                          etc.
	 * @param url   The URL of the document to be processed, 
	 *              in UTF-8 String representation.
	 * @param headers   A HashMap of the HTTP headers 
	 *                  retrieved with this document. 
	 *                  The HashMap key is the name of the 
	 *                  header and the HashMap value is 
	 *                  the header value.  All keys and 
	 *                  values are UTF-8 String 
	 *                  representations.
	 * @param statusCodeLine  The first line from the HTTP
	 *                        response for this document, 
	 *                        including the protocol, status
	 *                        code, and status description
	 *                        (i.e., "HTTP/1.1 200 OK"), as
	 *                        a UTF-8 String.
	 * 
	 * @return   The results of your analysis of this document, 
	 *           as a byte array.
	 */
	public byte[] processDocument(byte[] documentContent, String url, Map<String, String> headers, Map<Default80AppPropertyKeys, Object> default80AppProperties, String statusCodeLine) {

			return documentContent;
	}
	
	
	
	/*
	 	// EXAMPLE CODE: This method is used in the 
	 	// example code above.
	 	 
		public static String byteArrayToString(byte[] b, int byteStart, int numBytes) throws Exception
		{
			return new String (b, byteStart, numBytes, "UTF-8");
		}
	*/
		
}
